package com.qingfeng.provide.controller;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.dao.qingfeng.CouponsDao;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.qingfeng.Coupons;
import com.qingfeng.provide.constant.MyConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 优惠券
 * @author:LWY
 * @date: 2017/11/15 16:05
 * @modified:
 */
@RestController
@RequestMapping("/coupons")
public class CouponsController {
    @Autowired
    CouponsDao couponsDao;

    /**
     * 获取优惠券
     *
     * @param state 0:全部,2:可用,3:不可用
     * @return
     */
    @RequestMapping("/{state}")
    public ResultData get(HttpServletRequest request, @PathVariable Integer state) {
        String user_id = request.getParameter("id");
        String pageNum = request.getParameter("pageNum");
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }
        PageInfo<Coupons> pageInfo = couponsDao.get(Integer.valueOf(pageNum), MyConstant.getPageSize(), state, user_id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "获取优惠券成功", pageInfo);
        return resultData;
    }


    /**
     * 兑换优惠码
     *
     * @param coupons_id 优惠码
     * @return
     */
    @RequestMapping("/exchange/{coupons_id}")
    public ResultData exchange(HttpServletRequest request, @PathVariable String coupons_id) {
        String user_id = request.getParameter("id");
        couponsDao.exchange(coupons_id, user_id);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "兑换成功", "");
        return resultData;
    }

}
